#include "login_server.h"
#include <QSettings>

Login_Server::Login_Server(Http_Get* parent)
	: Http_Get(parent)
{

}

Login_Server::~Login_Server()
{

}

void Login_Server::getV2(QString redirect_uri)
{
	QString url = redirect_uri.replace(WX_SERVER_HOST, WX_V2_SERVER_HOST) + "&fun=old";

#if QWX_DEBUG
	qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << url;
#endif

	Http_Get::get(url);
}

QString Login_Server::getTicket()
{
	QSettings cookie(QWXDIR + "/" + COOKIE_FILENAME, QSettings::NativeFormat);

#if QWX_DEBUG
	qDebug() << cookie.allKeys();
#endif

	return cookie.value("webwx_data_ticket").toString();
}

QString Login_Server::getUin()
{
	QSettings cookie(QWXDIR + "/" + COOKIE_FILENAME, QSettings::NativeFormat);

	return cookie.value("wxuin").toString();
}

QString Login_Server::getSid()
{
	QSettings cookie(QWXDIR + "/" + COOKIE_FILENAME, QSettings::NativeFormat);

	return cookie.value("wxsid").toString();
}

bool Login_Server::isV2()
{
	QSettings cookie(QWXDIR + "/" + COOKIE_FILENAME, QSettings::NativeFormat);

	return cookie.value("v2").toBool();
}

bool Login_Server::exists()
{
	return QFile::exists(QWXDIR + "/" + COOKIE_FILENAME);
}

void Login_Server::finished(QNetworkReply* reply)
{
	QString replyStr(reply->readAll());
	QString uinStr = "";
	QString sidStr = "";
	QString ticketStr = "";
	QString expires = "";
	QString domain = "";
	QString path = "";
	QString webwxuvid = "";

#if QWX_DEBUG
	qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
	qDebug() << "DEBUG:" << replyStr;
#endif

	QString cookiePath = QWXDIR + "/" + COOKIE_FILENAME;
	QDir qwxDir(QWXDIR);
	if (!qwxDir.exists(cookiePath))
		qwxDir.mkdir(QWXDIR);

	QFile file(cookiePath);
	if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) {
		qWarning() << "ERROR: fail to save cookie!";
		return;
	}

	QTextStream out(&file);
	QLocale locale = QLocale(QLocale::C, QLocale::AnyCountry);

	Q_FOREACH (const QNetworkCookie cookie, Http_Get::cookies()) {
		if (Http_Get::cookies().size() > 1) {
			out << QString(cookie.name()) << "=" << QString(cookie.value())
				<< "; expires=" << locale.toString(cookie.expirationDate(),
				   "ddd, dd-MMM-yyyy hh:mm:ss") + " GMT"
				<< "; domain=" << cookie.domain()
				<< "; path=" << cookie.path() << endl;
		}

		if (QString(cookie.name()) == "webwxuvid")
			webwxuvid = QString(cookie.value());

		expires = locale.toString(cookie.expirationDate(),
								  "ddd, dd-MMM-yyyy hh:mm:ss") + " GMT";
		domain = cookie.domain();
		path = cookie.path();
		if (cookie.name() == "wxuin")
			uinStr = QString(cookie.value());
		else if (cookie.name() == "wxsid")
			sidStr = QString(cookie.value());
		else if (cookie.name() == "webwx_data_ticket")
			ticketStr = QString(cookie.value());
	}

	// TODO: so if webwx V1 fail to get uin and sid, switch to V2
	if (uinStr == "" || sidStr == "" || ticketStr == "") {
		QString href = "window.location.href=";
		int index = replyStr.indexOf(href);
		if (index == -1)
			qWarning() << "V2 still use XML format for cookie?";

		Q_EMIT login_error();
	} else {
		Q_EMIT infoChanged(uinStr, sidStr, ticketStr);
	}
	file.close();
}
